package com.ec.log.modules.log.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ec.common.constant.CommonConstant;
import com.ec.core.common.core.base.BasePageVO;
import com.ec.log.modules.log.dto.QueryLogDTO;
import com.ec.log.modules.log.entity.SysLog;
import com.ec.log.modules.log.mapper.SysLogMapper;
import com.ec.log.modules.log.service.ISysLogService;
import com.ec.log.modules.log.vo.SysLogVO;
import com.ec.core.mybatis.utils.CorePageUtil;
import com.ec.core.secure.utils.SecureUtil;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * @author Eric Lee
 * @Description:
 * @time: 2021/8/21 16:07
 */
@Service
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLog> implements ISysLogService {
    @Resource
    private SysLogMapper sysLogMapper;

    @Override
    public BasePageVO<SysLogVO> listLog(QueryLogDTO listLogDTO) {
        List<SysLogVO> list = Lists.newArrayList();
        if (Objects.isNull(listLogDTO.getTenantId())) {
            listLogDTO.setTenantId(SecureUtil.userTenantId());
        }
        int count = sysLogMapper.listLogCount(listLogDTO);
        if (count > 0) {
            list = sysLogMapper.listLog(listLogDTO);
        }
        return CorePageUtil.buildPageResult(list, count, SysLogVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeLog(Long logId) {
        SysLog sysLog = new SysLog();
        sysLog.setLogId(logId);
        sysLog.setTenantId(SecureUtil.userTenantId());
        return removeById(sysLog);
//        LambdaUpdateWrapper<SysLog> updateWrapper = Wrappers.<SysLog>lambdaUpdate()
//                .set(SysLog::getIsDelete, CommonConstant.ALREADY_DELETED)
//                .eq(SysLog::getLogId, logId)
//                .eq(SysLog::getTenantId, SecureUtil.userTenantId());
//        return this.update(updateWrapper);
    }
}
